/**
 * 通用uni-app网络请求
 * 基于 Promise 对象实现更简单的 request 使用方式，支持请求和响应拦截
 **/
export default {
	config: {
		baseUrl: "",
		timeout: 60000,
	},
	requestConfig: {
		data: {},
		header: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		method: "GET",
		dataType: 'json',
		responseType: 'text',
		sslVerify: true,
		withCredentials: false,
		firstIpv4: false,
	},
	uploadFileConfig: {
		files: null, // 需要上传的文件列表
		fileType: null, // 文件类型 image/video/audio
		file: null, // 要上传的文件对象
		filePath: null, // 上传文件资源的路径
		name: null, // 文件对应的 key
		formData: {}, // HTTP 请求中其他额外的 form data
	},
	downloadFileConfig: {
		filePath: null, // 指定文件下载后存储的路径 (本地路径)
	},
	interceptor: {
		invoke: null,
		success: null,
		fail: null,
		complete: null,
	},
	Task: null,
	request(options, _interceptor, name) {
		return new Promise((resolve, reject) => {
			let _config = Object.assign({}, this.config, this[`${name}Config`], options)

			// 请求拦截
			if (this.interceptor.invoke) {
				_config = this.interceptor.invoke(_config)
			}
			if (_interceptor && _interceptor.invoke) {
				_config = _interceptor.invoke(_config)
			}

			// 配置请求url
			_config.url = _config.baseUrl + _config.url
			
			delete _config.baseUrl

			// 请求成功拦截
			_config.success = response => {
				if (_interceptor && _interceptor.success) {
					return _interceptor.success(response).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				}
				if (this.interceptor.success) {
					this.interceptor.success(response).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				}
			}

			// 请求失败拦截
			_config.fail = response => {
				if (_interceptor && _interceptor.fail) {
					return _interceptor.fail(response).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				}
				if (this.interceptor.fail) {
					this.interceptor.fail(response).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				}
			}

			// 请求响应拦截
			_config.complete = response => {
				if (_interceptor && _interceptor.complete) {
					return _interceptor.complete(response).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				}
				if (this.interceptor.complete) {
					this.interceptor.complete(response).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				}
			}

			this.Task = uni[name](_config);
		});
	},
	get(url, data, options, interceptor) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options, interceptor, 'request')
	},
	post(url, data, options, interceptor) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options, interceptor, 'request')
	},
	put(url, data, options, interceptor) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options, interceptor, 'request')
	},
	delete(url, data, options, interceptor) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options, interceptor, 'request')
	},
	upload(url, options, interceptor) {
		if (!options) {
			options = {}
		}
		options.url = url
		return this.request(options, interceptor, 'uploadFile')
	},
	download(url, options, interceptor) {
		if (!options) {
			options = {}
		}
		options.url = url
		return this.request(options, interceptor, 'downloadFile')
	}
}
